package cn.com.surker.base;

import cn.com.surker.common.BaseGroupValidated;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;
import java.util.Date;

/**
 * 公共实体类
 *
 * @author luotao
 * @date 2024/9/5
 */
@Data
public abstract class BaseEntity  extends BaseGroupValidated implements Serializable {

	private static final long serialVersionUID = 1L;

	@Value("${erp.org.code}")
	@TableField(exist = false)
	protected String orgCode;

	/**
	 * 状态(0:启用1：禁用)
	 */
	protected Integer status;

	/**
	 * 备注
	 */
	protected String remark;

	/**
	 * 在原有的基础上新增信息
	 *
	 * @param remark
	 */
	public void addRemark(String remark) {
		if (StringUtils.isBlank(this.remark)) {
			this.remark = remark;
		} else {
			this.remark += ";" + remark;
		}
	}

	/**
	 * 创建人
	 */
	protected String creator;

	/**
	 * 创建时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	protected Date createTime;

	/**
	 * 修改人
	 */
	protected String updator;

	/**
	 * 修改时间
	 */
	@TableField(fill = FieldFill.UPDATE, update = "now()")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	protected Date updateTime;

	/**
	 * 验证数据是否存在
	 *
	 * @param entity
	 * @return
	 */
	public static boolean validateExist(BaseEntity entity) {
		if (entity == null || !StateEnum.ENABLED.getCode().equals(entity.getStatus())) {
			return false;
		}
		return true;
	}

	protected String baseInitCode(String prefix) {
		Date nowDate = new Date();
		String ymdh = DateUtil.convertString(nowDate, "yyMMdd");
		prefix += "-" + ymdh;
		// String s = RandomUtils.numString(4);
		String s = DateUtil.convertString(nowDate, "HHmmss");
		prefix += "-" + s;
		return prefix;
	}
}
